﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class SampleSaveViewModel:NodeViewModel
    {
        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bottle.png", UriKind.RelativeOrAbsolute);
        }

        public SampleSaveViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "SampleSaveViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            Init();
        }

        public SampleSaveViewModel() { Init(); }

        public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();
       
        #region Properties

        private string _errorMSG = null;
        public string ErrorMSG
        {
            get { return _errorMSG; }
            set
            {
                _errorMSG = value;
                NotifyChanged("ErrorMSG");
            }
        }

        private string _selectAllName;

        public string SelectAllName
        {
            get { return _selectAllName; }

            set
            {
                _selectAllName = value;
                NotifyChanged("SelectAllName");
            }
        }

        private bool _isMaterial;

        public bool IsMaterial
        {
            get { return _isMaterial; }

            set
            {
                _isMaterial = value;
                NotifyChanged("IsMaterial");
               
            }
        }

        private bool _isStatus;

        public bool IsStatus
        {
            get { return _isStatus; }

            set
            {
                _isStatus = value;
                NotifyChanged("IsStatus");

            }
        }

        private bool _selectAll;

        public bool SelectAll
        {
            get { return _selectAll; }

            set
            {
                _selectAll = value;
                NotifyChanged("SelectAll");
                // MakeSelectAll();
            }
        }

        private List<MaterielInfo> materialList;
        public List<MaterielInfo> MaterialList
        {
            get { return materialList; }
            set
            {
                materialList = value;
                NotifyChanged("MaterialList");
            }
        }

        private ObservableCollection<SampleSave> sampleSaveList;
        public ObservableCollection<SampleSave> SampleSaveList
        {
            get { return sampleSaveList; }
            set
            {
                sampleSaveList = value;
                NotifyChanged("SampleSaveList");
            }
        }

        private int _selectedStatusIndex;
        public int SelectedStatusIndex
        {
            get { return _selectedStatusIndex; }
            set
            {
                _selectedStatusIndex = value;
                NotifyChanged("SelectedStatusIndex");
               
            }
        }

        private MaterielInfo _selectMaterial;
        public MaterielInfo SelectMaterial
        {
            get { return _selectMaterial; }
            set
            {
                _selectMaterial = value;
                NotifyChanged("SelectMaterial");

            }
        }

        private DateTime _dpBegin;

        public DateTime dpBegin
        {
            get { return _dpBegin; }
            set
            {
                _dpBegin = value;
                NotifyChanged("dpBegin");
            }
        }

        private DateTime _dpEnd;

        public DateTime dpEnd
        {
            get { return _dpEnd; }
            set
            {
                _dpEnd = value;
                NotifyChanged("dpEnd");
            }
        }


        #endregion

        #region Command

        public void Init()
        {
            dpBegin = DateTime.Now.AddDays(-7);
            dpEnd = DateTime.Now;
            this.MaterialList = sp.SavedMaterial().ToList();
            this.IsMaterial = false;
            this.IsStatus = false;
            this.SelectAll = false;
            this.SelectAllName = "全选";
            GetList();
        }

        public void GetList()
        {
            Guid? mid;
            if (SelectMaterial != null)
                mid = SelectMaterial.ID;
            else mid = null;
            var list = sp.BottlesSaved(dpBegin, dpEnd, IsMaterial, mid, IsStatus, SelectedStatusIndex);
            sampleSaveList = new ObservableCollection<SampleSave>();
            foreach (var item in list)
            {
                var ss = new SampleSave();
                ss.IsSelected = false;
                ss.ID = item.ID;
                ss.AnalysBatchID = item.AnalysGroup.质检凭证号;
                ss.Materiel = item.AnalysGroup.物料.Name;
                ss.BottleID = item.BottleID;
                ss.SampleType = sc.DicSampleType[item.SampleType];
                ss.BottleStatus = sc.DicBottleStatus[item.BottleStatus];
                ss.SampleWeight = item.SampleWeight;
                ss.BottleSaveTime = item.BottleSaveTime;
                ss.BottleXYZ = item.BottleXYZ;
                ss.SaveDays = ss.BottleSaveTime != null ? (DateTime.Now - ss.BottleSaveTime.Value).Days : 9999;
                sampleSaveList.Add(ss);
            }
            this.SampleSaveList = sampleSaveList;
        }

        public void MakeSelectAll()
        {
            if (sampleSaveList != null && _selectAll == false)
            {
                foreach (var item in sampleSaveList)
                {
                    item.IsSelected = true;
                }
                _selectAll = true;
                _selectAllName = "全不选";
            }
            else if (sampleSaveList != null && _selectAll == true)
            {
                foreach (var item in sampleSaveList)
                {
                    item.IsSelected = false;
                }
                _selectAll = false;
                _selectAllName = "全选";
            }
            this.SampleSaveList = sampleSaveList;
            SelectAll = _selectAll;
            SelectAllName = _selectAllName;
        }

        public ICommand SelectOrUnSelectAll
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    MakeSelectAll();
                });
            }
        }

        public ICommand Query
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    GetList();
                });
            }
        }


        public ICommand Get
        {
            get
            {
                return new DelegateCommand(() =>
                {
                  
                    int i = 0;
                    this.ErrorMSG = null;
                    sampleSaveList = this.SampleSaveList;
                    foreach (var item in sampleSaveList)
                    {
                        if (item.IsSelected)
                        {
                            //调用接口发送取样瓶指令

                            if (sp.SetBottleStatus(item.ID, 6)) i++;

                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条取瓶指令！";

                });
            }
        }

        public ICommand Drop
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    //调用接口发送弃样指令
                    int i = 0;
                    this.ErrorMSG = null;
                    sampleSaveList = this.SampleSaveList;
                    foreach (var item in sampleSaveList)
                    {
                        if (item.IsSelected)
                        {
                            //调用接口发送取样瓶指令

                            if (sp.SetBottleStatus(item.ID, 4)) i++;

                        }
                    }
                    GetList();
                    if (i > 0) this.ErrorMSG = "已下达" + i.ToString() + "条弃样指令！";

                });
            }
        }

        #endregion
    }

    public class SampleSave:INPCBase
    {
        public SampleSave() { }

        private bool _isSelected = true;

        public bool IsSelected
        {
            get { return _isSelected; }

            set
            {
                _isSelected = value;
                NotifyChanged("IsSelected");
            }
        }

        public Guid ID { get; set; }

        public string AnalysBatchID { get; set; }
        public string Materiel { get; set; }

        public string BottleID { get; set; }

        public string SampleType { get; set; }

        public string BottleStatus { get; set; }

        public double SampleWeight { get; set; }

        public DateTime? BottleSaveTime { get; set; }

        public string BottleXYZ { get; set; }

        public int SaveDays { get; set; }
    }

}
